<template>
  <div class="app-container" v-if="!consumerHouseDesignLinkFormVisible && !consumerHouseDesignSchemeFormVisible && !consumerHouseDesignBudgetFormVisible">
    <div class="content-container">
      <div class="search-bar">
        <el-form ref="queryFormRef" :model="queryParams" :inline="true">
          <el-form-item prop="keywords" label="姓名">
            <el-input v-model="queryParams.name" placeholder="请输入姓名" clearable />
          </el-form-item>
          <el-form-item prop="keywords" label="联系方式">
            <el-input v-model="queryParams.mobile" placeholder="请输入联系方式" clearable />
          </el-form-item>

          <el-form-item>
            <el-button type="primary" icon="search" @click="handleSearch">搜索</el-button>
            <el-button icon="refresh" @click="handleResetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      <el-card shadow="never" class="table-card">
        <div class="table-card-body">
          <BasePageTable
            :table-data="tableData"
            :table-columns-data="tableColumnsData"
            :total="total"
            :loading="loading"
            @handleQuery="changePage"
          >
            <template #type="{ row, name }">
              {{ getLabelByValue(CONTACT_TYPE, row.type) }}
            </template>
            <!-- <template #couponCode="{ row }">
              {{ showCodeList(row.house.houseDesignLinkVO) }}
            </template> -->
            <template #designLinkStatus="{ row, name }">
              <el-tag :type="HOUSE_STATUS_MAP[row.house.designLinkStatus] || 'info'">
                {{ getLabelByValue(HOUSE_STATUS, row.house.designLinkStatus) }}
              </el-tag>
            </template>
            <template #designBudgetStatus="{ row, name }">
              <el-tag :type="HOUSE_STATUS_MAP[row.house.designBudgetStatus] || 'info'">
                {{ getLabelByValue(HOUSE_STATUS, row.house.designBudgetStatus) }}
              </el-tag>
            </template>
            <template #designSchemeStatus="{ row, name }">
              <el-tag :type="HOUSE_STATUS_MAP[row.house.designSchemeStatus] || 'info'">
                {{ getLabelByValue(HOUSE_STATUS, row.house.designSchemeStatus) }}
              </el-tag>
            </template>
            <template #operation="{ row, name }">
              <el-button
                link
                type="primary"
                icon="Edit"
                @click="handleEditConsumerHouseDesignLink(row)"
              >
                设计关联
              </el-button>
              <el-button
                link
                icon="Edit"
                type="primary"
                :disabled="row.house.designLinkStatus !== 2"
                @click="handleEditConsumerCouponBudget(row)"
              >
                核销券码
              </el-button>
              <el-button
                link
                type="primary"
                icon="Edit"
                @click="handleEditConsumerHouseDesignScheme(row)"
              >
                设计方案
              </el-button>
              <el-button
                link
                type="primary"
                icon="Edit"
                @click="handleEditConsumerHouseDesignBudget(row)"
              >
                品类预算
              </el-button>
            </template>
          </BasePageTable>
        </div>
      </el-card>
    </div>
  </div>
  <div class="app-container">
    <consumer-house-design-budget-form
      ref="consumerHouseDesignBudgetFormRef"
      v-if="consumerHouseDesignBudgetFormVisible"
      @back-to="consumerHouseDesignBudgetFormVisible = false"
      @load-data="handleQuery"
    ></consumer-house-design-budget-form>
    <consumer-house-design-link-form
      ref="consumerHouseDesignLinkFormRef"
      v-if="consumerHouseDesignLinkFormVisible"
      @back-to="consumerHouseDesignLinkFormVisible = false"
      @load-data="handleQuery"
    ></consumer-house-design-link-form>
    <consumer-house-design-scheme-form
      ref="consumerHouseDesignSchemeFormRef"
      v-if="consumerHouseDesignSchemeFormVisible"
      @back-to="consumerHouseDesignSchemeFormVisible = false"
      @load-data="handleQuery"
    ></consumer-house-design-scheme-form>
    <ConsumerHouseCouponForm
      ref="consumerHouseCouponForm"
      @load-data="handleQuery"
    ></ConsumerHouseCouponForm>
  </div>
</template>

<script setup lang="ts">
import BasePageTable from "@/components/BasePageTable/index.vue";
import HouseAPI, { HousePageQuery, HousePageVO } from "@/api/house/index";
import { getLabelByValue } from "@/utils/enums";
import {
  CONTACT_TYPE,
  GenderEnum,
  UserStatusEnum,
  UserTypeEnum,
} from "../../../enums/system/UserEnum";
import { BIND_STATUS_ENUM } from "@/enums/contact/ContactEnum";
import ConsumerHouseDesignLinkForm from "@/views/house/houseDesign/components/consumer-house-design-link-form.vue";
import ConsumerHouseDesignBudgetForm from "@/views/house/houseDesign/components/consumer-house-design-budget-form.vue";
import ConsumerHouseDesignSchemeForm from "@/views/house/houseDesign/components/consumer-house-design-scheme-form.vue";
import ConsumerHouseCouponForm from "./components/consumer-house-coupon-form.vue";
import { HOUSE_STATUS, HOUSE_STATUS_MAP } from "@/enums/house/house-const";

const loading = ref(false);
const consumerHouseDesignBudgetFormRef = ref();
const consumerHouseDesignBudgetFormVisible = ref(false);
const consumerHouseDesignLinkFormRef = ref();
const consumerHouseDesignLinkFormVisible = ref(false);
const consumerHouseDesignSchemeFormRef = ref();
const consumerHouseDesignSchemeFormVisible = ref(false);
const consumerHouseCouponForm = ref();
const total = ref(0);
const queryFormRef = ref();

const queryParams = reactive<HousePageQuery>({
  name: "",
  mobile: "",
  stepInfoList: ["STEP_DESIGN_LINK", "STEP_DESIGN_SCHEME", "STEP_DESIGN_BUDGET"],
  status: BIND_STATUS_ENUM.SUCCESS.value,
  userType: UserTypeEnum.DESIGNER.value,
  typeList: [CONTACT_TYPE.DESIGNER.value],
  pageNum: 1,
  pageSize: 10,
});
const tableData = ref<HousePageVO[]>([]);
const tableColumnsData = ref([
  {
    prop: "house.ownName",
    label: "户主名称",
    showOverflowTooltip: true,
  },
  {
    prop: "house.region",
    label: "小区地址",
    showOverflowTooltip: true,
  },
  {
    prop: "house.neighborhood",
    label: "小区名称",
    showOverflowTooltip: true,
  },
  {
    prop: "house.houseNumber",
    label: "门牌号",
    showOverflowTooltip: true,
  },
  {
    prop: "house.size",
    label: "面积（平方米）",
    width: 150,
    showOverflowTooltip: true,
  },
  {
    prop: "house.houseDesignLinkVO.couponCode",
    label: "代金券券码",
    showOverflowTooltip: true,
    width: 140,
    // customSlot: "couponCode",
  },
  {
    prop: "type",
    label: "通讯录类型",
    width: 150,
    showOverflowTooltip: true,
    customSlot: "type",
  },
  {
    prop: "house.designLinkStatus",
    label: "设计关联状态",
    width: 150,
    showOverflowTooltip: true,
    customSlot: "designLinkStatus",
  },
  {
    prop: "house.designSchemeStatus",
    label: "设计方案状态",
    width: 150,
    showOverflowTooltip: true,
    customSlot: "designSchemeStatus",
  },
  {
    prop: "house.designBudgetStatus",
    label: "品类预算状态",
    width: 150,
    showOverflowTooltip: true,
    customSlot: "designBudgetStatus",
  },
  {
    prop: "operation",
    label: "操作",
    width: 400,
    align: "center",
    fixed: "right",
    customSlot: "operation", // 自定义插槽名称
  },
]);
// 查询
function handleQuery() {
  loading.value = true;
  HouseAPI.getHouseContactPage(queryParams)
    .then((data) => {
      tableData.value = data.list || [];
      total.value = data.total;
    })
    .finally(() => {
      loading.value = false;
    });
}

function handleSearch() {
  queryParams.pageNum = 1;
  handleQuery();
}

function changePage(val: any) {
  queryParams.pageNum = val.pageNum;
  queryParams.pageSize = val.pageSize;
  handleQuery();
}

// 重置查询
function handleResetQuery() {
  queryFormRef.value.resetFields();
  queryParams.pageNum = 1;
  queryParams.name = "";
  queryParams.mobile = "";
  handleQuery();
}

function showCodeList(val) {
  console.log(val);

  let data = val?.couponCode;
  console.log(data);

  if (data && data !== null) {
    let names = data.map((item) => item.couponCode);
    return names.join(",");
  } else {
    return "";
  }
}

async function handleEditConsumerHouseDesignBudget(val) {
  let cannotEdit = val.house.designBudgetStatus === HOUSE_STATUS.APPROVED.value;
  consumerHouseDesignBudgetFormVisible.value = true;
  await nextTick();
  consumerHouseDesignBudgetFormRef.value.showModal(val, cannotEdit);
}
async function handleEditConsumerHouseDesignLink(val) {
  let cannotEdit = val.house.designLinkStatus === HOUSE_STATUS.APPROVED.value;
  consumerHouseDesignLinkFormVisible.value = true;
  await nextTick();
  consumerHouseDesignLinkFormRef.value.showModal(val, cannotEdit);
}
async function handleEditConsumerHouseDesignScheme(val) {
  let cannotEdit = val.house.designSchemeStatus === HOUSE_STATUS.APPROVED.value;
  consumerHouseDesignSchemeFormVisible.value = true;
  await nextTick();
  consumerHouseDesignSchemeFormRef.value.showModal(val, cannotEdit);
}
async function handleEditConsumerCouponBudget(val) {
  val.pageName = "Design";
  let cannotEdit = false;
  consumerHouseCouponForm.value.showModal(val, cannotEdit);
}

// function isApproved(val) {
//   return (
//     val.designLinkStatus === HOUSE_STATUS.APPROVED.value &&
//     val.designBudgetStatus === HOUSE_STATUS.APPROVED.value &&
//     val.designSchemeStatus === HOUSE_STATUS.APPROVED.value
//   );
// }

onMounted(async () => {
  handleQuery();
});
</script>
